$(function ($) {
    var miFn={
        int:function () {
            miFn.routerFn();
            // miFn.scrollFn();
        },
        routerFn:function () {
            var router = new Router({
                container: '#container'
            });
            router.push({
                url: '/index',
                className: 'home',
                render: function (callback){
                    $("#container").html("");
                    huiju.common.ajaxGet('data.json','',function (data) {
                        var html=template('mi-index',data.data);
                        callback(null,html);
                        //console.debug(data);
                    })
                },
                bind:function () {
                    $(".mi-tabbar").show();
                    $(".swiper-container").swiper({
                        loop: true,
                        autoplay: 3000
                    });
                    var load=false;
                    var _navTop=$(".mi-Search").height();
                    $(window).scroll(function () {
                        if (!load){
                            HuiJuCommon.prototype.hideloading();
                            huiju.common.ajaxGet('data1.json','',function (data) {
                                var html=template('mi-index',data.data);
                                $(".home").append(html);
                                $(".calssify-box").hide();
                                $(".mi-nav-classify").hide();
                            });
                            load=true;
                        }
                        var scrollTop=$("body").scrollTop();
                        /*  console.debug(_navTop,scrollTop);*/
                        if (_navTop<=scrollTop){
                            $(".mi-Search").css("background-color","rgb(229, 131, 53)")
                        }else{
                            $(".mi-Search").css("background-color","");
                        }
                    });
                    $(".mi-tabbar >a").removeClass("weui-bar__item--on");
                    $(".mi-tabbar >a").eq(0).addClass("weui-bar__item--on");
                    $(".mi-tabbar >a").click(function () {
                        $(this).addClass("weui-bar__item--on").siblings().removeClass("weui-bar__item--on");
                    });
                }
            }).push({
                url: '/product/category',
                className: 'category',
                render: function (callback){
                    $("#container").html("");
                    huiju.common.ajaxGet('package.json','',function (data) {
                        var html=template('mi-index',{sections:data.data});
                        callback(null,html);
                    })
                },
                bind:function () {
                    $(".mi-tabbar").show();
                    var _navTop=$(".mi-nav-classify").offset().top;
                    var $title=$('.category-list-name')
                    var $nav_ul=$('.mi-nav-classify ul')
                    var $nav_li=$('.mi-nav-classify ul li')
                    var _index=0
                    console.log('calssify-title====',$title)
                    $nav_ul.on('click','li',function (){
                        console.debug(this)
                        console.debug($nav_li.index(this))
                        var top_li=$title.eq($nav_li.index(this)).offset().top
                        $(window).scrollTop(top_li-310)
                    })
                    var isMove = false;
                    var startX = 0;
                    var ul_left = 0;
                    var distanceX = 0;
                    $nav_ul.on('touchstart',function (e){
                        console.debug(e.originalEvent.changedTouches[0].clientX)
                        console.debug(e.originalEvent.targetTouches[0].clientX)
                        startX = e.originalEvent.changedTouches[0].clientX;
                        ul_left = $nav_ul.position().left;
                        console.log(startX)
                        console.log(ul_left)
                        return false
                    }).on('touchmove',function (e){
                        var moveX = e.originalEvent.changedTouches[0].clientX-startX
                        // distanceX = moveX - startX;
                        console.log('move',moveX)
                        var left=ul_left+moveX
                        if (left>$nav_ul.width()-$(window).width()){
                            left=$nav_ul.width()-$(window).width()
                        }
                        if (left>0){
                            left=0
                        }
                        $nav_ul.css({left:left})
                        return false
                    }).on('touchend','li',function (e){
                        console.log('结束')
                        // var top_li=$title.eq($nav_li.index(this)).offset().top
                        // $(window).scrollTop(top_li-310)
                        return false
                    })
                    // miFn.scrollFn( $nav_ul, function(e){
                    //     console.log('左滑动')
                    // }, function(e){
                    //     console.log('右滑动')
                    // }  )
                    $(window).scroll(function () {
                        // var scrollTop=$("body").scrollTop();
                        var scrollTop=$('html,body').scrollTop()
                        // console.log('ffrrfrf')
                        // console.log(_navTop,scrollTop)
                        // console.log('======================')
                        if (_navTop<=scrollTop){
                            $(".mi-nav-classify").addClass("fixed");
                        }else{
                            $(".mi-nav-classify").removeClass("fixed");
                        }
                        var $c_title=null
                        console.log($title.length)
                        $title.each(function (){
                            var tt=$(this).offset().top
                            if (tt>=scrollTop&&tt<=scrollTop+$(window).height()){
                                $c_title=$(this)
                            }
                        })
                        if ($c_title!==null){
                            var tmp=$title.index($c_title[0])
                            if (_index!==tmp){
                                $nav_li.removeClass('on')
                                $nav_ul.animate({left:-$nav_li.eq(tmp).addClass('on').position().left},500)
                                console.debug('position',$nav_li.eq(tmp).addClass('on').position().left)
                                console.debug('tmp',tmp)
                            }
                            _index=tmp
                        }
                    })
                    $(".mi-tabbar >a").removeClass("weui-bar__item--on");
                    $(".mi-tabbar >a").eq(1).addClass("weui-bar__item--on");
                    $(".mi-tabbar >a").click(function () {
                        $(this).addClass("weui-bar__item--on").siblings().removeClass("weui-bar__item--on");
                    });

                }

            }).push({
                url: '/cart/index',
                className: 'category',
                render: function (callback){
                    $("#container").html("");
                    huiju.common.ajaxGet('recommendBlank.json','',function (data) {
                        var html=template('cartContent',data.data);
                        callback(null,html);
                    })
                },
                bind:function () {
                    $(".mi-tabbar").show();
                    $(".mi-tabbar >a").removeClass("weui-bar__item--on");
                    $(".mi-tabbar >a").eq(2).addClass("weui-bar__item--on");
                    $(".mi-tabbar >a").click(function () {
                        $(this).addClass("weui-bar__item--on").siblings().removeClass("weui-bar__item--on");
                    });

                }
            }).push({
                url: '/user/index',
                className: '',
                render: function (){
                    var html=template('miUser',{});
                    document.getElementById('container').innerHTML = html;
                },
                bind:function () {
                    $(".mi-tabbar").show();
                    $(".mi-tabbar >a").removeClass("weui-bar__item--on");
                    $(".mi-tabbar >a").eq(3).addClass("weui-bar__item--on");
                    $(".mi-tabbar >a").click(function () {
                        $(this).addClass("weui-bar__item--on").siblings().removeClass("weui-bar__item--on");
                    });
                }
            }).push({
                url: '/search/index',
                className: 'search',
                render: function (callback){
                    huiju.common.ajaxGet('search.json','',function (data) {
                        var html=template('searchContent',data.data);
                        callback(null,html);
                    })
                },
                bind:function () {
                    $(".s-box").focus();
                    $(".mi-tabbar").hide();
                }
            }).setDefault('/index').init();
        },
        //通知滚动效果
        scrollFn:function (dom, leftCallBack, rightCallback) {
                // 判断手势的条件
                // 必须滑动 滑动距离超过10px
                var isMove = false;
                var startX = 0;
                var distanceX = 0;

                dom.on( 'touchstart', function(e){
                    startX = e.touchs[0].clientX;
                })

                dom.on('touchmove', function(e){
                    isMove = true;
                    var moveX = e.touchs[0].clientX;
                    distanceX = moveX - startX;
                })

                dom.on('touchend',function(e){
                    if(isMove && Math.abs(distanceX >= 10)){
                        // 右滑动
                        if(distanceX>0){
                            rightCallback && rightCallback.call(this,e);
                        }
                        // 左滑动
                        else{
                            leftCallBack && leftCallBack.call(this,e);
                        }
                    }
                    // 重置参数
                    isMove = false;
                    startX = 0;
                    distanceX = 0;
                })
        }
    }
    miFn.int();
})
